<?php
class BaseCmsWidgetLayout extends CComponent
{
    /**
    * CmsWidget instance which use this layout to render contnent
    * 
    * @var BaseCmsWidget
    */
    protected $owner;
    
    /**
    * Data to be render
    * 
    * @var array
    */
    public $data;
    
    /**
    * Configure layout with widget's layout settings
    * 
    * @param array $config
    */
    public function config($config){
        foreach($config as $key => $value){
            if (!isset($this->settingParams[$key]))
                throw new CException($key.' is not defined in the Setting Parameter list of '.get_class($this));
            $this->settingParams[$key]['value'] = $value;
        }
    }
    
    /**
    * Get setting parameter as property
    * 
    * @param string $name
    * @return mixed
    */
    public function __get($name) {
        if (isset($this->settingParams[$name]))
            return $this->settingParams[$name]['value'];
        else
            return parent::__get($name);
    }
    
    public function hasParam($name) {
        return array_key_exists($name, $this->settingParams);
    }
    
    public function setOwner($value){
        if ($value instanceof BaseCmsWidget)
            $this->owner = $value;
    }
    /**
    * Module owns the widget
    * @return string
    */
    public function getModuleId(){
        $class=new ReflectionClass(get_class($this));
        $path = explode(DIRECTORY_SEPARATOR, dirname($class->getFileName()));
        // it is faster to do a search from the widget's folder up to root
        $path = array_reverse($path);
        $modules = array_keys(Yii::app()->modules);
        foreach($path as $folder)
            if (array_search($folder, $modules) !== false)
                return $folder;
    }
    
    /**
    * Yii path of the widget
    * @return string
    */
    public function getClassPath(){
        return $this->getModuleId().'.components.widgets.'.get_class($this);
    }
    
    
    public function getViewPath(){
        throw new Exception('Class '.get_class($this).' must extend getViewPath() in BaseCmsWidgetLayout.');
    }
    
    /**
    * Derived class should override this function to render content
    * @return string HTML output
    */
    public function render(){
        $viewPath = $this->getViewPath();
        return $this->owner->render($viewPath, $this->data, true);
    }
    
    /////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////                ////////////////////////////////////
    /////////////////////////////////   PARAMETERS   ////////////////////////////////////
    /////////////////////////////////                ////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////
    
    /**
    * Override this property to define parameter for the widget
    * 
    * @var array
    */
    protected $settingParams = array(
//            'name' => array(
//                'type' => CHtml::function or Yii path to a widget. 
                            //If CHtml function then the function name is in lower case. 
                            //Suffixes like 'field' or 'button' must be removed
//                'rules' => array('rule name - see Yii rule classes' => array(config)),
//                'items' => array(key => value) //used for dropDownList of similar CHtml functions
//                'htmlOptions' => array()
//                // following items are used to setup the widget setting info while installing  
//                'label'           => 'Short friendly param name'
//                'value'           => 'default value'
//                'description'     => 'Full description'
//                'setting_group'   => 'General Settings'
//                'ordering'        => 0
//                'visible'         => 0
//            ),
    );
    
    /**
    * Derived class for a specific widget may extend this function
    * to provide preference options for user. The returned array of
    * setting parameters should match with the widget parameters defined
    * in the __widget_setting table.
    * 
    */
    public function getSettingParams() {
        return $this->settingParams;
    }
}
?>
